package com.sunyard.manage.initRunner;

import cn.hutool.core.lang.Assert;
import com.sunyard.dal.bo.warning.NotWorkingTimeInitBo;
import com.sunyard.dal.dto.OrgTreeDTO;
import com.sunyard.dal.mapper.OperateHistoryMapper;
import com.sunyard.dal.mapper.OrganizationMapper;
import com.sunyard.redisUtil.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.sunyard.utils.DateUtil.getStringTime;

@Component
@Slf4j
public class InitOperatorHistory {

    @Value("${operator.history.cron}")
    private String cronOp = "0 0 23 * * ?";
    @Autowired
    OperateHistoryMapper operateHistoryMapper;

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    OrganizationMapper organizationMapper;

    /*@PostConstruct
    public void init() {
        setRedisOperatorHistory();
    }*/

    public void setRedisOperatorHistory(){
        List<OrgTreeDTO> allOrg = organizationMapper.getAllOrg();
        for (OrgTreeDTO orgTreeDTO : allOrg) {
            List<String> lists = new ArrayList<>();
            getAllChild(allOrg, lists, orgTreeDTO.getOrgId());
            lists.add(orgTreeDTO.getOrgId());
            //非工作日志时间查询
            String stringTime = getStringTime();
            NotWorkingTimeInitBo notWorkingTimeInitBo = new NotWorkingTimeInitBo();
            notWorkingTimeInitBo.setEndTime(stringTime);
            notWorkingTimeInitBo.setOrgIds(lists);
            int notWorkingTimeLogCount= operateHistoryMapper.getNotWorkingTimeLogCountByOrg(notWorkingTimeInitBo);
            redisUtils.set("operatorHistory:"+orgTreeDTO.getOrgId(), notWorkingTimeLogCount);
        }
    }

    @Scheduled(cron = "${operator.history.cron}")
    public void cron(){
        log.info("----定时任务开始执行-----");
        setRedisOperatorHistory();
        log.info("----定时任务执行结束-----");
    }

    public List<String> getListOrgIds(String orgId) {
        String strkey = "orgid:" + orgId;
        List<String> lists = null;
        lists = redisUtils.get(strkey, List.class);
        if (lists == null) {
            lists = new ArrayList<>();
            recursiveGetOrgId(lists, orgId);
            redisUtils.set(strkey, lists);
        }

        return lists;
    }

    public void getAllChild(List<OrgTreeDTO> allOrgs, List<String> lists, String orgId) {
        List<OrgTreeDTO> collect = allOrgs.stream().filter(s -> s.getOrgPid().equals(orgId)).collect(Collectors.toList());
        while (collect.isEmpty()) {
            return;
        }
        for (OrgTreeDTO childOrgId : collect) {
            lists.add(childOrgId.getOrgId());
            recursiveGetOrgId(lists, childOrgId.getOrgId());
        }
    }


    //递归查询子机构
    private void recursiveGetOrgId(List<String> lists, String orgId) {
        List<String> orgIdList = organizationMapper.getOrgIdsList(orgId);
        while (orgIdList.isEmpty()) {
            return;
        }
        for (String childOrgId : orgIdList) {
            lists.add(childOrgId);
            recursiveGetOrgId(lists, childOrgId);
        }
    }
}
